package com.food.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.dao.service.ApplicationService;
import com.food.dao.service.CollectService;
import com.food.dao.service.CourseService;
import com.food.dao.service.MbproductService;
import com.food.dao.service.MessageService;
import com.food.dao.service.OrderService;
import com.food.dao.service.RecipeService;
import com.food.model.Application;
import com.food.model.Collect;
import com.food.model.Mbproduct;
import com.food.model.Member;
import com.food.model.Message;
import com.food.model.Order;
import com.food.model.Recipe;

@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String type = request.getParameter("type");
		
		HttpSession session = request.getSession();
		Member member = (Member)session.getAttribute("user");
		
		if(type.equals("RECIPE")){
			int rid = Integer.parseInt(request.getParameter("rid"));
			RecipeService recipeService = new RecipeService();
			Recipe recipe = new Recipe();
			recipe.setRid(rid);
			recipeService.deleteRecipe(recipe);
			RequestDispatcher rd = request
					.getRequestDispatcher("/member/MyRecipe");
			rd.forward(request, response);
			return;
		}
		
		if(type.equals("COLLECT")){
			int rid = Integer.parseInt(request.getParameter("rid"));
			CollectService collectService = new CollectService();
			Collect collect = new Collect();
			collect.setMbid(member.getMbid());
			List<Collect> list = collectService.findCollectByMb(collect);
			for(Collect cl:list){
				if(cl.getRid()==rid){
					collectService.deleteCollect(cl);
					RequestDispatcher rd = request
							.getRequestDispatcher("/member/MyCollect");
					rd.forward(request, response);
					return;
				}
			}
		}
		
		if(type.equals("ORDER")){
			int hismbid = Integer.parseInt(request.getParameter("hismbid"));
			OrderService orderService = new OrderService();
			Order order = new Order();
			order.setMymbid(member.getMbid());
			List<Order> list = orderService.findOrderByMb(order);
			for(Order od:list){
				if(od.getHismbid()==hismbid){
					orderService.deleteOrder(od);
					RequestDispatcher rd = request
							.getRequestDispatcher("/member/MyOrder");
					rd.forward(request, response);
					return;
				}
			}
		}
		if(type.equals("MyCourse")){
			int cid = Integer.parseInt(request.getParameter("cid"));
			ApplicationService applicationService = new ApplicationService();
			Application application = new Application();
			application.setMbid(member.getMbid());
			List<Application> list = applicationService.findApplicationByMb(application);
			for(Application a:list){
				if(a.getCid()==cid){
					applicationService.deleteApplication(a);
					RequestDispatcher rd = request
							.getRequestDispatcher("/member/MyCourse");
					rd.forward(request, response);
					return;
				}
			}
		}
		if(type.equals("MyProduct")){
			int pid = Integer.parseInt(request.getParameter("pid"));
			MbproductService mbproductService = new MbproductService();
			Mbproduct mbproduct = new Mbproduct();
			mbproduct.setMbid(member.getMbid());
			List<Mbproduct> list = mbproductService.findProductByMb(mbproduct);
			for(Mbproduct m:list){
				if(m.getPid()==pid){
					mbproductService.deleteMbProduct(m);
					RequestDispatcher rd = request
							.getRequestDispatcher("/member/MyProduct");
					rd.forward(request, response);
					return;
				}
			}
		}
		if(type.equals("MESSAGE")){
			int messageid = Integer.parseInt(request.getParameter("messageid"));
			int rid = Integer.parseInt(request.getParameter("rid"));
			MessageService messageService = new MessageService();
			Message message = new Message();
			message.setMbid(member.getMbid());
			List<Message> list = messageService.findMessageByMb(message);
			for(Message m:list){
				if(m.getMessageid()==messageid){
					messageService.deleteMessage(m);
					RequestDispatcher rd = request
							.getRequestDispatcher("/recipe/ShowRecipe?rid="+rid);
					rd.forward(request, response);
					return;
				}
			}
		}
		
		
	}
	
}
